package com.jy.rhin.infra.repo.document.submission.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.jy.rhin.infra.repo.document.submission.po.DocumentEntryPO;
import com.jy.rhin.sdk.dto.document.statistic.GetTotalGroupDayRespDto;
import com.jy.rhin.sdk.dto.document.statistic.GetTotalGroupTypeRespDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author dhl
 * @date 2022/6/1
 * @Description
 */
@Mapper
@Repository
public interface DocumentEntryMapper extends BaseMapper<DocumentEntryPO> {

    /**
     * 统计指定日期区间每日总数
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("SELECT DATE(creation_time) AS riQi,     \n" +
            "       COUNT(id)           AS riZengShu \n" +
            "FROM t_document_entry t                 \n" +
            "WHERE t.creation_time >= #{startTime}    \n" +
            "  AND t.creation_time < #{endTime}     \n" +
            "GROUP BY DATE(creation_time)            \n" +
            "ORDER BY riQi                           \n")
    List<GetTotalGroupDayRespDto> queryDocumentEntryTotalGroupDay(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 根据文档类型查询总数以及日增数
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Select("SELECT COUNT(t.id)                                                                          AS zongShu,   \n" +
            "       COUNT(if(t.creation_time >= #{startTime} AND t.creation_time < #{endTime}, 1, null)) AS riZengShu, \n" +
            "       t.type_code                                                                          AS fenLeiCode \n" +
            "FROM t_document_entry t                                                                                   \n" +
            "GROUP BY t.type_code                                                                                      \n" +
            "ORDER BY zongShu DESC                                                                                     \n")
    List<GetTotalGroupTypeRespDto> queryDocumentEntryTotalGroupType(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 列表查询
     * @param queryWrapper
     * @return
     */
    @Select("SELECT DISTINCT a.id,                                                           \n" +
            "       a.submission_id                 AS submissionId,                         \n" +
            "       a.format_code                   AS formatCode,                           \n" +
            "       a.title,                                                                 \n" +
            "       a.patient_id                    AS patientId,                            \n" +
            "       a.huan_zhe_xm                   AS huanZheXm,                            \n" +
            "       a.zheng_jian_hm                 AS zhengJianHm,                          \n" +
            "       a.service_start_time            AS serviceStartTime,                     \n" +
            "       a.service_stop_time             AS serviceStopTime,                      \n" +
            "       a.creation_time                 AS creationTime,                         \n" +
            "       a.status_code                   AS statusCode,                           \n" +
            "       a.unique_id                     AS uniqueId,                             \n" +
            "       a.repository_unique_id          AS repositoryUniqueId,                   \n" +
            "       a.home_community_id             AS homeCommunityId,                      \n" +
            "       a.class_code                    AS classCode,                            \n" +
            "       a.healthcare_facility_type_code AS healthcareFacilityTypeCode,           \n" +
            "       a.practice_setting_code         AS practiceSettingCode,                  \n" +
            "       a.type_code                     AS typeCode,                             \n" +
            "       a.org_id                        AS orgId,                                \n" +
            "       a.org_name                      AS orgName                               \n" +
            "FROM t_document_entry a ${ew.customSqlSegment}")
    List<DocumentEntryPO> queryDocumentEntryListByCoding(@Param(Constants.WRAPPER) QueryWrapper<DocumentEntryPO> queryWrapper);

}
